package com.longrise.android.byjk.plugins.tabfirst.homeAd;


import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.tabfirst.HomeViewJumpHelper;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by callxkj on 2018/3/9.
 * 首页广告弹窗
 */

public class HomeAdDialogFragment extends DialogFragment implements View.OnClickListener {


    private View mRootView;
    ImageView mIv;
    private ImageView mIvdelete;
    private List<AdDialogBean> mAdList = new ArrayList<>();

    public static HomeAdDialogFragment newInstance(Parcelable[] dialogbeans) {
        HomeAdDialogFragment addialogFragment = new HomeAdDialogFragment();
        Bundle args = new Bundle();
        args.putParcelableArray("dialogBeans", dialogbeans);
        addialogFragment.setArguments(args);
        return addialogFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        Bundle arguments = getArguments();
        if (arguments != null) {
            Parcelable[] dialogBeanses = arguments.getParcelableArray("dialogBeans");
            mAdList.clear();
            if (dialogBeanses != null) {
                for (int x = 0; x < dialogBeanses.length; x++) {
                    mAdList.add((AdDialogBean) dialogBeanses[x]);
                }

            }
        }

        mRootView = inflater.inflate(R.layout.dialogfragment_home_ad, null);
        mIv = (ImageView) mRootView.findViewById(R.id.dialog_fragment_ad_iv);
        mIvdelete = (ImageView) mRootView.findViewById(R.id.dialog_fragment_ad_iv_delete);
        return mRootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //显示第一张广告
        if (mAdList.size() > 0) {
            AdDialogBean dialogBean = mAdList.get(0);
            String localpath = dialogBean.getLocalpath();
            if (!TextUtils.isEmpty(localpath)) {
                Glide.with(getContext())
                        .load(localpath)
                        .into(mIv);
            }
        }

        initEvent();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.TOP;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.dimAmount = 0.0f;
        window.setAttributes(params);
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mIv.getLayoutParams();
        double width = AppUtil.getScreenWidth() * 280.0 / 375;
        layoutParams.width = (int) (AppUtil.getScreenWidth() * 280.0 / 375);
        layoutParams.height = (int) (width * 300.0 / 280);
        mIv.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams ivdeleteParams = (RelativeLayout.LayoutParams) mIvdelete.getLayoutParams();
        ivdeleteParams.addRule(RelativeLayout.ALIGN_RIGHT, mIv.getId());
        ivdeleteParams.addRule(RelativeLayout.ABOVE, mIv.getId());
        ivdeleteParams.setMargins(0, 0, 0, AppUtil.dip2px(16));
        mIvdelete.setLayoutParams(ivdeleteParams);

    }


    private void initEvent() {
        mIvdelete.setOnClickListener(this);
        mIv.setOnClickListener(this);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_BACK){
                    return true;
                }
                return false;
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_fragment_ad_iv_delete:
                changeImageAndDismiss();
                break;
            case R.id.dialog_fragment_ad_iv:
                imgClick();
                break;
        }
    }

    /**
     * 如果还有未显示的image 则显示未显示的  如果显示完了直接dismissdialog
     */
    private void changeImageAndDismiss() {
        if (mAdList.size() > 1) {
            mAdList.remove(0);
            AdDialogBean dialogBean = mAdList.get(0);
            String localpath = dialogBean.getLocalpath();
            if (!TextUtils.isEmpty(localpath)) {
                Glide.with(getContext())
                        .load(localpath)
                        .into(mIv);
            }
        } else {
            getDialog().dismiss();
        }
    }

    /**
     * 图片点击
     */
    private void imgClick(){
        AdDialogBean dialogBean = mAdList.get(0);
        EntityBean bean = new EntityBean();
        bean.set("linkedtype",dialogBean.getLinkedtype());
        bean.set("linkedurl",dialogBean.getLinkedurl());
        bean.set("urltype",dialogBean.getUrltype());
        bean.set("title",dialogBean.getTitle());
        HomeViewJumpHelper.BannerJump(getContext(),bean);
    }
}
